<?php
/**
 * register new member
 */
class RegisterController extends Controller{
    //
    public function actionIndex(){
        $model = new User;

        // Uncomment the following line if AJAX validation is needed
        $this->performAjaxValidation($model);

        if (isset($_POST['User'])) {
            $model->attributes = $_POST['User'];

            //set the type
            $model->type = User::TYPE_MEMBER;

            //check the upload file
            if(($fileUpload = CUploadedFile::getInstanceByName('avatar'))){
                if(($uploadFileName = UploadHelper::saveAvatar($fileUpload))){
                    $model->avatar = $uploadFileName;
                }
            }

            if ($model->save())
                $this->redirect(array('view', 'id' => $model->user_id));
        }

        $this->render('/profile/register', array(
            'model' => $model
        ));
    }


    /**
     * Performs the AJAX validation.
     * @param CModel the model to be validated
     */
    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'user-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
}